package com.liuqing.www.http程序读取;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.servlet.http.Cookie;
public class HttpRead {
public static Map<String,String> xx=new HashMap<String,String>();
private static String title="";
public static String path="http://192.168.1.200:8080/chuhan/index.jsp";
//public static String path="http://www.fkjava.com/article/1/2107117382228659708.html";
private static String ContentType="Content-Type";
private static String Content="application/x-www-form-urlencoded;charset=GBK";
static{
////for(int i=0;i<10;i++){
////	title+=title;
////}
//xx.put("title", title);
//xx.put("textContent", title);
//xx.put("editor", "");
////xx.put("htmlContent", title);
//System.getProperties().put("proxySet","true");
//
//System.getProperties().setProperty("http.proxyHost", "218.26.204.66");
//System.getProperties().setProperty("http.proxyPort", "8080");

}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
	try {
//		ExecutorService threadpool=Executors.newCachedThreadPool()  ;//始终只有2个线程在运行
//		for(int i=0;i<6000;i++){
//			MyThread t=new MyThread(i+"");
//			threadpool.execute(t);
//		}
		new MyThread("").start();
		new MyThread("").start();
		new MyThread("").start();
//		 System.out.println(HttpRead.URLPost("http://www.fkjava.com/qa-save.htm",HttpRead.xx));
	} catch (Exception e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
}
	}
	private static final String URL_PARAM_CONNECT_FLAG = "&";
	/**
	 * url为地址，map装的为要传递的参数
	 * @param strUrl
	 * @param map
	 * @return
	 * @throws IOException
	 */
	public static List<String> URLGet(String strUrl,Map<String,String> map) throws IOException{
		String strtTotalURL = "";
		List<String> list = new ArrayList<String>();
		strtTotalURL = strUrl;//+"?"+getUrl(map);
//		System.out.println(strtTotalURL);
		URL url = new URL(strtTotalURL);
		HttpURLConnection con = (HttpURLConnection)url.openConnection();
		con.setRequestProperty("Connection", "keep-alive");
		con.setRequestProperty("keep-alive", "30000");
		con.setRequestProperty(Content, ContentType);
		con.setRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows 2000)");
		con.setUseCaches(false);//浏览器是否缓存页面
		con.setFollowRedirects(true);//支持服务器器重定向
		BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
//		while(true){
//			String line = in.readLine();
//			if(line ==null){
//				break;
//			}
//			else list.add(line);
//		}
		
		in.close();
		return list;
	}
	
	public static List<String> URLPost(String strUrl,Map<String,String> map) throws IOException{
		String content = "";
		content = getUrl(map);
		
		URL url = new URL(strUrl);
		HttpURLConnection con = (HttpURLConnection)url.openConnection();
		con.setDoInput(true);//URL 连接可用于输入和/或输出。如果打算使用 URL 连接进行输入，则将 DoInput 标志设置为 true；如果不打算使用，则设置为 false。默认值为 true。 
		con.setDoOutput(true);
		con.setAllowUserInteraction(true);
		con.setUseCaches(false);
		con.setRequestMethod("POST");
		con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=GBK");
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(con.getOutputStream()));
		bw.write(content);
		bw.flush();
		bw.close();
//		BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
		List<String> list = new ArrayList<String>();
//		while (true) {
//			String line = br.readLine();
//			if (line == null) {
//				break;
//			} else if (line.equals("")) {
//				continue;
//			} else {
//				list.add(line);
//			}
//		}
		System.gc();
		return list;
	
	}
	/**
	 * 重写url用？xx=xx
	 * @param map
	 * @return
	 */
	static String getUrl(Map<String,String> map){
		if(map == null||map.size()==0){
			return("");
		}
		StringBuffer url = new StringBuffer();
		Set<String> keys = map.keySet();
		for(Iterator i = keys.iterator();i.hasNext();){
			String key = String.valueOf(i.next());
			Object val  =map.get(key);
			String str  =val!=null?val.toString():"";
			try {
				str = URLEncoder.encode(str,"GBK");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			url.append(key).append("=").append(str).append(URL_PARAM_CONNECT_FLAG);
		}
		String strURL ="";
		strURL = url.toString();
		
		if(URL_PARAM_CONNECT_FLAG.equals(""+strURL.charAt(strURL.length()-1))){
			strURL = strURL.substring(0,strURL.length()-1);
		}
		return strURL;
	}
public static void operaCookie(){
//	Cookie cook=new Cookie("username","lq123456789000");
//	response.addCookie(cook);
//	for(Cookie cook:request.getCookies()){
//		System.out.println(cook.getName());
//		System.out.println(cook.getValue());
//		System.out.println(cook.getPath());
//	}
}
}
class MyThread extends Thread{
	public MyThread(String name){
//		setName(name);
		super(name);
	}
	public void run(){//重新run方法
		
		while(true){
			 try {
//		 System.out.println(getName() + " 线程运行开始!");
				List<String> li= HttpRead.URLGet(HttpRead.path,HttpRead.xx);
//				for(String str:li){
//					 System.out.println(str);
//				}
		System.out.println("成功");
		 Thread.sleep(100);
		 System.gc();
		} catch (Exception e) {
			e.printStackTrace();
			continue;
			// TODO Auto-generated catch block
//			e.printStackTrace();
		}
		}
	}
	public  static void  setIP(){
		
	}
}